﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 2. Write a JavaScript function to check if in a given 
			expression the brackets are put correctly.
			Example of correct expression: ((a+b)/5-d).
			Example of incorrect expression: )(a+b)). -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		function CheckBrackets(input) {
			var res = true;
			var str = new String(input);

			var open = 0;
			for (var i = 0; i < str.length; i++) {
				if (str[i] == '(') {
					open++;
				}
				else {
					if (str[i] == ')') {
						if (open == 0) {
							res = false;
							break;
						}
						else {
							open--;
						}
					}
				}
			}

			if (res && open > 0) {
				res = false;
			}

			return res;
		}

		// User input
		var input = prompt("Enter some text", "((a+b)/5-d)");

		// Output
		jsConsole.writeLine("You have entered: " + input);
		jsConsole.writeLine("Bracket are put correctly = " +
								CheckBrackets(input));
	</script>
</body>
</html>
